﻿using HalconDotNet;
using Halcon二次开发.Model.自定义控件;
using Halcon二次开发.View.中心距;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Halcon二次开发.Controller.中心距合格检测
{
    public class SpacedetectionController
    {
        //当前窗体句柄
        public HwindowFlowNodeUrseControl hwindowFlowNode;
        //卡尺数据数据
        public double[,] linesList;
        //距离转换系数
        public double Sum3;
        //输入图像
        public HObject HideIntputImage;
        //窗体对象
        public DistanceForm distanceForm;
        //线段第一个点的行数据
        public double[] rowsLinesPoint;
        //线段第一个点的列数据
        public double[] columnsLinesPoint;
        //线段排序好之后的数据
        public double[,] linst;
        //线段所有中心点的集合
        public double[,] centerPoints;
        //输出结果
        public List< bool> HideOutputResults;

        public Dictionary<int, int> noIndex;

        /// <summary>
        /// 动作类的构造方法
        /// </summary>
        public SpacedetectionController(DistanceForm distanceForm, HwindowFlowNodeUrseControl hwindowFlowNode)
        {
            this.distanceForm= distanceForm;
            this.hwindowFlowNode= hwindowFlowNode;
        }

        public SpacedetectionController()
        {
        }

        /// <summary>
        /// 运行方法
        /// </summary>
        //numericUpDown8最大接受值，最小接受值
        private double minNumber = 0;
        private double maxNumber = 0;
        public void Run()
        {
            //排序方法
            SortLineList();
            //排序之后就是计算点与点之间的距离(求中心点)
            FindCentrePoint();
            if (centerPoints == null) return;
            if (centerPoints.GetLength(0)>1)
            {

                HideOutputResults = new List<bool>(linesList.GetLength(0));
                Object[,] LsRegions = new Object[linesList.GetLength(0), 2];

                distanceForm.dataGridView1.Rows.Clear();
                //distance_pp(Row1, Column1, Row2, Column2, Distance)
                for (int i=0; i<centerPoints.GetLength(0); i++)
                {
                    HTuple distance = 0;
                    if (i== centerPoints.GetLength(0)-1)
                    {
                        HOperatorSet.DistancePp(centerPoints[i, 0], centerPoints[i, 1], centerPoints[i - 1, 0], centerPoints[i- 1, 1], out distance);
                    }
                    else
                    {
                        HOperatorSet.DistancePp(centerPoints[i, 0], centerPoints[i, 1], centerPoints[i + 1, 0], centerPoints[i + 1, 1], out distance);
                    }
                    if (i == 0)
                    {
                        minNumber = (double)distanceForm.numericUpDown1.Value - (double)distanceForm.numericUpDown8.Value;
                        maxNumber = (double)distanceForm.numericUpDown1.Value + (double)distanceForm.numericUpDown8.Value;
                    }
                    if (((double)distance.D * Sum3) <= maxNumber && ((double)distance.D * Sum3) >= minNumber)//合格
                    {
                        //合格进行模板中心显示文本
                        HOperatorSet.SetTposition(hwindowFlowNode.hSmartWindowControl1.HalconWindow, centerPoints[i, 0] + 35, centerPoints[i, 1] + 15);
                        //设置字体
                        HOperatorSet.SetFont(hwindowFlowNode.hSmartWindowControl1.HalconWindow, "宋体" + "-Normal-" + 10);
                        //设置字体颜色
                        HOperatorSet.SetColor(hwindowFlowNode.hSmartWindowControl1.HalconWindow, "green");
                        HOperatorSet.WriteString(hwindowFlowNode.hSmartWindowControl1.HalconWindow, "间距:" + ((double)distance.D * Sum3).ToString("F3"));


                        //记录合格数据
                        HideOutputResults.Add(true);
                    }
                    else//不合格
                    {
                        //合格进行模板中心显示文本
                        HOperatorSet.SetTposition(hwindowFlowNode.hSmartWindowControl1.HalconWindow, centerPoints[i, 0] + 35, centerPoints[i, 1] + 15);
                        //设置字体
                        HOperatorSet.SetFont(hwindowFlowNode.hSmartWindowControl1.HalconWindow, "宋体" + "-Normal-" + 10);
                        //设置字体颜色
                        HOperatorSet.SetColor(hwindowFlowNode.hSmartWindowControl1.HalconWindow, "red");
                        HOperatorSet.WriteString(hwindowFlowNode.hSmartWindowControl1.HalconWindow, "间距:" + ((double)distance.D * Sum3).ToString("F3"));

                        //记录不合格数据
                        HideOutputResults.Add( false);
                    }
                    //显示当前间距
                    distanceForm.dataGridView1.Rows.Add(i, ((double)distance.D * Sum3));
                    //当前间距        distance
                }
            }
        }
        /// <summary>
        /// 对线的一端进行排序,2种排序方式
        /// </summary>
        public void SortLineList()
        {
            try
            {
                if (linesList == null) return;
                if (linesList.GetLength(0) > 0)
                {
                    rowsLinesPoint = new double[linesList.GetLength(0)];
                    columnsLinesPoint = new double[linesList.GetLength(0)];
                    linst = new double[linesList.GetLength(0), 4];
                    for (int i = 0; i < linesList.GetLength(0); i++)
                    {
                        rowsLinesPoint[i] = linesList[i, 0];
                        columnsLinesPoint[i] = linesList[i, 1];
                    }
                }
                if (distanceForm.comboBox3.SelectedItem.Equals("行排序"))
                {
                    noIndex = new Dictionary<int, int>();
                    //对第一个的行坐标进行排序
                    HOperatorSet.TupleSort(rowsLinesPoint, out HTuple sorteds);

                    for (int i = 0; i < sorteds.Length; i++)
                    {
                        for (int j = 0; j < linesList.GetLength(0); j++)
                        {
                            if (sorteds[i].D == linesList[j, 0])
                            {
                                linst[i, 0] = linesList[j, 0];
                                linst[i, 1] = linesList[j, 1];
                                linst[i, 2] = linesList[j, 2];
                                linst[i, 3] = linesList[j, 3];
                                //  i : 新下标   j: 原下标
                                noIndex.Add(i, j);
                            }
                        }
                    }
                }
                else if (distanceForm.comboBox3.SelectedItem.Equals("列排序"))
                {
                    noIndex = new Dictionary<int, int>();
                    //对第一个的列坐标进行排序
                    HOperatorSet.TupleSort(columnsLinesPoint, out HTuple sorteds);
                    for (int i = 0; i < sorteds.Length; i++)
                    {
                        for (int j = 0; j < linesList.GetLength(0); j++)
                        {
                            if (sorteds[i].D == linesList[j, 1])
                            {
                                linst[i, 0] = linesList[j, 0];
                                linst[i, 1] = linesList[j, 1];
                                linst[i, 2] = linesList[j, 2];
                                linst[i, 3] = linesList[j, 3];
                                //  i : 新下标   j: 原下标
                                noIndex.Add(i, j);
                            }
                        }
                    }

                }
            }
            catch(Exception ex)
            {
                throw new Exception("排序方式错误");
            }
        }
        /// <summary>
        /// 找中心点的位置
        /// </summary>
        public void FindCentrePoint()
        {
            if (linst == null) return;
            centerPoints = new double[linst.GetLength(0),2];
            ////linst；所有点在这个集合当中
            for (int i=0;i<linst.GetLength(0);i++)
            {
                double row = (linst[i,0]+ linst[i, 2])/2;
                double clo = (linst[i, 1] + linst[i, 3]) / 2;
                centerPoints[i,0] = row;
                centerPoints[i,1] = clo;
            }
        }

        /// <summary>
        /// 工具运行
        /// </summary>
        public void Tool_Run() {
            if (HideIntputImage == null)
            {
                throw new Exception("当前图像为空");
            }
            hwindowFlowNode.hSmartWindowControl1.HalconWindow.ClearWindow();
            HOperatorSet.DispObj(HideIntputImage, hwindowFlowNode.hSmartWindowControl1.HalconWindow);
            Run();
        }

        //public void HardRun(HSmartWindowControl hSmartWindowControl,DistanceForm distansceForm)
        //{
        //    //排序方法
        //    SortLineList();
        //    //排序之后就是计算点与点之间的距离(求中心点)
        //    FindCentrePoint();
        //    if (centerPoints == null) return;
        //    if (centerPoints.GetLength(0) > 1)
        //    {

        //        HideOutputResults = new List<bool>(linesList.GetLength(0));`
        //        Object[,] LsRegions = new Object[linesList.GetLength(0), 2];

        //        distanceForm.dataGridView1.Rows.Clear();
        //        //distance_pp(Row1, Column1, Row2, Column2, Distance)
        //        for (int i = 0; i < centerPoints.GetLength(0); i++)
        //        {
        //            HTuple distance = 0;
        //            if (i == centerPoints.GetLength(0) - 1)
        //            {
        //                HOperatorSet.DistancePp(centerPoints[i, 0], centerPoints[i, 1], centerPoints[i - 1, 0], centerPoints[i - 1, 1], out distance);
        //            }
        //            else
        //            {
        //                HOperatorSet.DistancePp(centerPoints[i, 0], centerPoints[i, 1], centerPoints[i + 1, 0], centerPoints[i + 1, 1], out distance);
        //            }
        //            if (i == 0)
        //            {
        //                minNumber = (double)distanceForm.numericUpDown1.Value - (double)distanceForm.numericUpDown8.Value;
        //                maxNumber = (double)distanceForm.numericUpDown1.Value + (double)distanceForm.numericUpDown8.Value;
        //            }
        //            if (((double)distance.D * Sum3) <= maxNumber && ((double)distance.D * Sum3) >= minNumber)//合格
        //            {
        //                //合格进行模板中心显示文本
        //                HOperatorSet.SetTposition(hwindowFlowNode.hSmartWindowControl1.HalconWindow, centerPoints[i, 0] + 35, centerPoints[i, 1] + 15);
        //                //设置字体
        //                HOperatorSet.SetFont(hwindowFlowNode.hSmartWindowControl1.HalconWindow, "宋体" + "-Normal-" + 10);
        //                //设置字体颜色
        //                HOperatorSet.SetColor(hwindowFlowNode.hSmartWindowControl1.HalconWindow, "green");
        //                HOperatorSet.WriteString(hwindowFlowNode.hSmartWindowControl1.HalconWindow, "间距:" + ((double)distance.D * Sum3).ToString("F3"));


        //                //记录合格数据
        //                HideOutputResults.Add(true);
        //            }
        //            else//不合格
        //            {
        //                //合格进行模板中心显示文本
        //                HOperatorSet.SetTposition(hwindowFlowNode.hSmartWindowControl1.HalconWindow, centerPoints[i, 0] + 35, centerPoints[i, 1] + 15);
        //                //设置字体
        //                HOperatorSet.SetFont(hwindowFlowNode.hSmartWindowControl1.HalconWindow, "宋体" + "-Normal-" + 10);
        //                //设置字体颜色
        //                HOperatorSet.SetColor(hwindowFlowNode.hSmartWindowControl1.HalconWindow, "red");
        //                HOperatorSet.WriteString(hwindowFlowNode.hSmartWindowControl1.HalconWindow, "间距:" + ((double)distance.D * Sum3).ToString("F3"));

        //                //记录不合格数据
        //                HideOutputResults.Add(false);
        //            }
        //            //显示当前间距
        //            distanceForm.dataGridView1.Rows.Add(i, ((double)distance.D * Sum3));
        //            //当前间距        distance
        //        }
        //    }
        //}
    }
}
